<?php

require_once './_AddinLibraries/database_driver.php';

function library_generate_row($querry_result, $argument) {
  $row = array();
  $record_ = $querry_result->fetchAssoc();
  $i = 0;
  while ($record_ != NULL) {
    
    foreach ($argument as $index => $content) {
      $row[$i][] = $record_[$content];      
    }

    $record_ = $querry_result->fetchAssoc();
    ++$i;
  }
  return $row;
}

function library_generate_field($table,&$upper_list) {
  select_itec_database();
  $list_argurment = array();

  $query_field = db_query("SHOW FIELDS FROM ".$table);
    while ($res = $query_field->fetchAssoc()) { 
         $upper_list[] = $res['field'];
         $list_argurment[] = strtolower($res['field']);
     }
  select_drupal_database();
  return $list_argurment;
}

function classID_list_query() {
    select_itec_database();
    $classes = array();
    $sql = db_query("call sp_GetListOfClass(null, null)");
    while ($erg = $sql->fetchAssoc()) {
        if ($erg == NULL)
            break;
        $classes[] = $erg['classid'];
    }; 
    select_drupal_database();
    return $classes;
}

function status_list_query() {
    select_itec_database();
    $desp = array();
    $sql = db_query("SELECT description FROM student_status");
    while ($erg = $sql->fetchAssoc()) {
        if ($erg == NULL)
            break;
        $desp[] = $erg['description'];
    };
    select_drupal_database();
    return $desp;
}

function add_student($studentID, $firstname, $lastname, $dateofbirth, $sex, $class, $status, $email, $ssn, $phone, $currentaddress, $permanentaddress){
    select_itec_database();
    
    $query = 
     "call sp_AddStudent('".$studentID."', null, '".$firstname."', '".$lastname."', '".$dateofbirth."',".$sex.", '".$class."',". $status.", '".$email."', '".$ssn."','".$phone."','".$currentaddress."','".$permanentaddress."',null)";
    $sql = db_query($query);
    $result = $sql->fetchAssoc();
    
    select_drupal_database();
    return $result['result'];
}

function get_student_info($id) {
    select_itec_database();
    $sql = db_query("call sp_GetStudentInfo('".$id."')");
    $result = $sql->fetchAssoc();
    select_drupal_database();
    return $result;
}

function get_gender($id) {
    if($id == 1)
        return 1;
    else if($id == -1)
        return 0;
}

function get_student_status_from_id($id) {
    select_itec_database();
    $result = array();
    $sql = db_query("call sp_GetStudentStatus()");
    $i = -1;
    while($result = $sql->fetchAssoc()) {
        ++$i;
        if($result['statusid'] == $id)
            break;
    }
    select_drupal_database();
    
    return $i;
}

function get_program_by_class($id) {
    select_itec_database();
    $sql = db_query("call sp_GetProgramByClass('".$id."')");
    $result = $sql->fetchAssoc();
    select_drupal_database();
    
    return $result['programname'];
}

